using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.ChatRoom;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class ForwardContactsPage : BasePage
	{
		public enum AddPeopleType
		{
			CreateFromChatPage,
			CreateFromMainPage
		}
		private const string TAG = "ForwardContactsPage";
		private ApplicationBar mBar;
		public string cuUser;
		private ForwardPeopleComplete mAddPeopleComplete;
		private List<string> FilterList;
		private EventWatcher mCreateFromMainPageWatcher;
		private bool isShowLoading;
		private List<string> validList;
		private bool doCancel;
		internal Grid LayoutRoot;
		internal panelTitle2Line panelTitle;
		internal Grid ContentPanel;
		internal ContactsView mContactView;
		private bool _contentLoaded;
		public ForwardContactsPage()
		{
			base.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.AddPeoplePage_BackKeyPress));
			this.InitializeComponent();
			base.set_ApplicationBar(this.GetApplicationBar());
			ContactsView.resetCheckedList();
		}
		~ForwardContactsPage()
		{
		}
		private ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			applicationBarIconButton.set_Text(strings.ChatForward_OK_Btn);
			applicationBarIconButton.add_Click(new EventHandler(this.OkMenuBtn_Click));
			applicationBarIconButton.set_IsEnabled(false);
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			return this.mBar;
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null)
			{
				if (object_0.Length == 3)
				{
					this.mAddPeopleComplete = (object_0[0] as ForwardPeopleComplete);
					this.FilterList = (object_0[1] as List<string>);
					if (this.FilterList != null)
					{
						ContactsView.resetCheckedList(this.FilterList);
					}
					this.cuUser = (object_0[2] as string);
					this.CreateFromMainPageInit();
					return;
				}
			}
			base.GoBack();
		}
		protected override void OnPageOut()
		{
			this.CreateFromMainPageUnint();
		}
		private void AddPeoplePage_BackKeyPress(object sender, CancelEventArgs e)
		{
			if (this.mAddPeopleComplete == null)
			{
				return;
			}
			this.mAddPeopleComplete(false, null);
		}
		private void OkMenuBtn_Click(object sender, EventArgs e)
		{
			if (this.FilterList == null)
			{
				Log.smethod_1("ForwardContactsPage", "OkMenuBtn_Click ,FilterList == null");
				return;
			}
			List<Contact> checkedItem = ContactsView.getCheckedItem();
			if (checkedItem != null && checkedItem.get_Count() > 0)
			{
				this.CreateFromMainPage(checkedItem);
				return;
			}
			MessageBoxManager.ShowToast(strings.ChatAddPeopleSelectAlert, 2500, null, false);
		}
		public void ItemCheckChanged(object sender, object object_0)
		{
			List<Contact> checkedItem = ContactsView.getCheckedItem();
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton();
			if (base.get_ApplicationBar() == null || base.get_ApplicationBar().get_Buttons().get_Count() == 0)
			{
				return;
			}
			applicationBarIconButton = (ApplicationBarIconButton)base.get_ApplicationBar().get_Buttons().get_Item(0);
			if (checkedItem.get_Count() == 0)
			{
				applicationBarIconButton.set_IsEnabled(false);
				return;
			}
			applicationBarIconButton.set_IsEnabled(true);
		}
		private void CreateFromMainPageInit()
		{
			Log.smethod_5("ForwardContactsPage", "registerEventWatcher CreateFromMainPageInit");
			this.mCreateFromMainPageWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onChatRoomEventHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS, this.mCreateFromMainPageWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR, this.mCreateFromMainPageWatcher);
		}
		private void CreateFromMainPageUnint()
		{
			Log.smethod_5("ForwardContactsPage", "removeEventWatcher CreateFromMainPageUnint");
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS, this.mCreateFromMainPageWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR, this.mCreateFromMainPageWatcher);
			this.mCreateFromMainPageWatcher = null;
		}
		private void CreateFromMainPage(List<Contact> conList)
		{
			if (conList == null)
			{
				return;
			}
			if (conList.get_Count() <= 0)
			{
				return;
			}
			if (this.isShowLoading)
			{
				return;
			}
			if (conList.get_Count() == 1)
			{
				ChatContainerPage.GoToChatPage(this, conList.get_Item(0).strUsrName, null, null, null);
				this.mAddPeopleComplete(true, conList.get_Item(0).strUsrName);
				return;
			}
			Log.smethod_5("ForwardContactsPage", "start create chat room");
			this.ShowLoading(strings.ChatCreateWaitInfo);
			List<string> list = new List<string>();
			using (List<Contact>.Enumerator enumerator = conList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					list.Add(current.strUsrName);
				}
			}
			ServiceCenter.sceneCreateChatRoomService.doScene(strings.ChatDefaultTopic, list);
		}
		private void onChatRoomEventHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			if (this.doCancel)
			{
				Log.smethod_5("ForwardContactsPage", "onChatRoomEventHandler:user docancel");
				return;
			}
			Log.smethod_5("ForwardContactsPage", "onChatRoomEventHandler:" + evtArgs.mEventID);
			switch (evtArgs.mEventID)
			{
			case EventConst.ON_NETSCENE_CREATE_CHATROOM_SUCCESS:
			{
				NetSceneCreateChatRoomResult netSceneCreateChatRoomResult = evtArgs.mObject as NetSceneCreateChatRoomResult;
				if (netSceneCreateChatRoomResult == null)
				{
					Log.smethod_1("ForwardContactsPage", "netSceneCreateChatRoomRet == null");
					this.HideLoading();
					return;
				}
				if (netSceneCreateChatRoomResult.sceneCreateChatRoom == null)
				{
					Log.smethod_1("ForwardContactsPage", "netSceneCreateChatRoomRet.sceneCreateChatRoom == null");
					this.HideLoading();
					return;
				}
				if (string.IsNullOrEmpty(netSceneCreateChatRoomResult.sceneCreateChatRoom.chatRoomName))
				{
					Log.smethod_1("ForwardContactsPage", "sceneCreateChatRoom.chatRoomName == null");
					this.HideLoading();
					return;
				}
				this.mAddPeopleComplete(true, netSceneCreateChatRoomResult.sceneCreateChatRoom.chatRoomName);
				this.GoToNewChatRoom(netSceneCreateChatRoomResult.sceneCreateChatRoom.chatRoomName);
				return;
			}
			case EventConst.ON_NETSCENE_CREATE_CHATROOM_ERR:
			{
				NetSceneCreateChatRoomResult netSceneCreateChatRoomResult2 = evtArgs.mObject as NetSceneCreateChatRoomResult;
				if (netSceneCreateChatRoomResult2 == null)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
					Log.smethod_1("ForwardContactsPage", "createChatRoomRet == null");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom == null)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
					Log.smethod_1("ForwardContactsPage", "netSceneCreateChatRoomRet.sceneCreateChatRoom == null");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList.get_Count() != 0)
				{
					this.HideLoading();
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.invalidUsernameList, strings.ChatUserNameError), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList.get_Count() != 0)
				{
					this.HideLoading();
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.notExistUserList, strings.ChatUserNameNoExist), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList.get_Count() != 0)
				{
					this.HideLoading();
					MMMessageBox.Show(this.getShowInfo(netSceneCreateChatRoomResult2.sceneCreateChatRoom.memeberBlackList, strings.ChatRefruseToAdd), true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.sceneCreateChatRoom.verifyUserList != null && netSceneCreateChatRoomResult2.sceneCreateChatRoom.verifyUserList.get_Count() != 0)
				{
					this.HideLoading();
					this.ValidToFriend();
					return;
				}
				if (netSceneCreateChatRoomResult2.retCode == RetConst.MM_ERR_CLIENT)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateNetError, true, "确定");
					return;
				}
				if (netSceneCreateChatRoomResult2.retCode == RetConst.MM_ERR_MEMBER_TOOMUCH)
				{
					this.HideLoading();
					MMMessageBox.Show(strings.ChatCreateOverMaxNum, true, "确定");
					return;
				}
				this.HideLoading();
				this.mAddPeopleComplete(false, null);
				MMMessageBox.Show(strings.ChatCreateErrorInfo, true, "确定");
				return;
			}
			default:
				return;
			}
		}
		private void GoToNewChatRoom(string chatRoomId)
		{
			Log.smethod_5("ForwardContactsPage", "GoToNewChatRoom:" + chatRoomId);
			ChatContainerPage.GoToChatPage(this, chatRoomId, null, null, null);
		}
		private string getShowInfo(List<string> userlist, string formatStr)
		{
			string text = "";
			if (userlist != null && userlist.get_Count() != 0)
			{
				for (int i = 0; i < userlist.get_Count(); i++)
				{
					Contact contact = StorageMgr.contact.method_0(userlist.get_Item(i));
					if (i == userlist.get_Count() - 1)
					{
						text += ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact));
					}
					else
					{
						text = text + ((contact == null) ? userlist.get_Item(i) : ContactHelper.GetContactDisplayName(contact)) + ",";
					}
				}
			}
			return text + formatStr;
		}
		public void ShowLoading(string info)
		{
			if (this.isShowLoading)
			{
				return;
			}
			this.isShowLoading = true;
			ProcessingCover.getProgressBar(this.LayoutRoot, info, null, false, false, null);
		}
		public void HideLoading()
		{
			if (!this.isShowLoading)
			{
				return;
			}
			this.isShowLoading = false;
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		public void ValidToFriend()
		{
			if (this.validList != null && this.validList.get_Count() != 0)
			{
				Dialog2BtnWithInputBox.Show(strings.ChatNeedValidTitle, strings.ChatNeedValidInfo, strings.COM_Confirm, strings.COM_Cancel, "", new Dialog2BtnWithInputBox.dismissWithClickBtn(this.SayHiDialogHandler), null, null);
				return;
			}
		}
		private void SayHiDialogHandler(object sender, int nClickIndex)
		{
			if (this.validList != null && this.validList.get_Count() != 0)
			{
				if (nClickIndex == 0 && sender != null && sender is Dialog2BtnWithInputBox)
				{
					Dialog2BtnWithInputBox dialog2BtnWithInputBox = sender as Dialog2BtnWithInputBox;
					string text = dialog2BtnWithInputBox.txtIn.get_Text();
					if (text == null || text.get_Length() == 0)
					{
						text = strings.ChatDefaultHello;
					}
					ServiceCenter.sceneVerifyUser.doSceneSendMultiRequests(this.validList, AddContactScene.MM_ADDSCENE_CHATROOM, text);
					MessageBoxManager.ShowToast(strings.ChatHelloSuccess, 2500, null, false);
				}
				return;
			}
		}
		public void DoCancel()
		{
			this.doCancel = true;
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			Log.smethod_5("ForwardContactsPage", "OnBackKeyPress");
			this.DoCancel();
			base.OnBackKeyPress(cancelEventArgs_0);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/forward/ForwardContactsPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.mContactView = (ContactsView)base.FindName("mContactView");
		}
	}
}
